<template>

  <div>
    <el-page-header @back="goBack" content="注册账号">
    </el-page-header>

    <div class="login-container">

      <div class="login-box">
        <h2 class="login-title" >注册</h2>
  
        <div class="form-group">
          <label for="username">账号</label>
          <input v-model="username" id="username" type="text" placeholder="请输入6-10位账号">
        </div>
  
        <div class="form-group">
          <label for="password">密码</label>
          <input v-model="password" id="password" type="password" placeholder="请输入6-10位存在特殊字符的密码">
        </div>
     
         
        <div class="form-group">
          <label for="secret_answer">你最喜欢食物是什么?</label>
          <input v-model="secret_answer" id="secret_answer" type="secret_answer" placeholder="请输入密保答案">
        </div>
  
        <button class="btn-login" @click="register">注册</button>
        <div class="login-help">
          <p v-if="error" class="error-message">{{ error }}</p>
        </div>
        
      </div>
    </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        username: '',
        password: '',
        secret_answer:'',
        error: null
      };
    },
    methods: {
      goBack() {
        this.$router.replace('/');
      },
      register() {
        if(!this.username ||!this.password || !this.secret_answer){
          alert("请填写完整信息");
          return
        }
        this.error = null;
  
        axios.post('http://localhost:8081/springboot01/user/register', 
    {
      username: this.username,
      password: this.password,
      secret_answer:this.secret_answer
    },
    {
      headers: {
        'Content-Type': 'application/json'
      }
    }
  )
  .then(response => {
    if(response.data.data===1){
  alert("注册成功")}else{
    alert(response.data.msg)
  }
  
  })
  .catch(error => {
    console.error('Login failed:', error);
    this.error = 'Login failed. Please check your username and password and try again.';
  });
      }
    }
  };
  </script>
  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
  }
  
  .login-box {
    width: 350px;
    background-color: #fff;
    padding: 40px;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }
  
  .login-title {
    font-size: 24px;
    margin-bottom: 20px;
    text-align: center;
  }
  
  .form-group {
    margin-bottom: 20px;
  }
  
  label {
    font-weight: bold;
  }
  
  input {
    width: 100%;
    padding: 10px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }

  .login-help {
    margin-top: 10px;
    text-align: center;
  }
  .error-message {
    color: red;
    margin-top: 10px;
    text-align: center;
  }
  button {
  width: 100%;
  height: 2.25rem;
  background-color: #ff9bad;
  color: white;
  border: none;
  border-radius: 3rem;
  font-size: 0.8em;
  font-weight: 700;
  cursor: pointer;
  transition: all .2s;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

button:hover {
  color: #ff9bad;
}

button:after {
  content: "";
  background: white;
  position: absolute;
  z-index: -1;
  left: -20%;
  right: -20%;
  top: 0;
  bottom: 0;
  transform: skew(-45deg) scale(0, 1);
  transition: all 0.5s;
}

button:hover:after {
  transform: skew(-45deg) scale(1, 1);
}

  </style>
  